package com.sdy.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.sdy.entity.User;
import com.sdy.mapper.UserMapper;
import com.sdy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: ssm-sm
 * @author: sdy
 * @create: 2022-12-06 10:19
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int login(User user){
        User user1 = userMapper.queryUser(user.getUsername());
        if (user1==null){
//            用户不存在
            return 0;
        }

        if (user1.getPassword().equals(SecureUtil.md5(user.getPassword()))){
//            登录成功
            return 1;
        }
//        密码错误
        return 2;
    }

//    注册
    @Override
    public int register(User user) {
        User user1 = userMapper.queryUser(user.getUsername());
        if (user1!=null){
//            用户已存在
            return 2;
        }
        user.setPassword(SecureUtil.md5(user.getPassword()));
        int i = userMapper.addUser(user);
        return i;
    }

    @Override
    public int updPassword(User user,String newPassword) {
        User user1 = userMapper.queryUser(user.getUsername());
        if (!user1.getPassword().equals(SecureUtil.md5(user.getPassword()))){
//            旧密码不正确
            return 2;
        }
        user1.setPassword(SecureUtil.md5(newPassword));
        int i = userMapper.updPassword(user1);
        return i;
    }

    @Override
    public String getImg(String username) {
        User user = userMapper.queryUser(username);
        String txFileName = user.getTxFileName();
        return txFileName;
    }
}
